<?php
namespace app\admin\controller;
use app\admin\model\KnowsModel;
use think\Controller;
use \think\Request;
use think\Session;

class  Knows extends Controller{
    /*
     育儿知识列表
     */
    public function index(){
        $data = [
            'is_home' =>input('s_ishome'),
            'is_vouch' =>input('s_isvouch'),
            'is_top' =>input('s_istop'),
            'parenting_title' =>input('parenting_title')
        ];
        $model = new KnowsModel();
        $res = $model->selectAll($data);
        $row = $model->select();
        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res',$res);
        $this->assign('row',$row);
        return $this->fetch();
    }

    /*
     *添加内容显示
     */
    public function addKnowsView(){
        $model = new KnowsModel();
        $res = $model->select();
//        echo '<pre>';
//        print_r($res);
//        exit;
        $this->assign('res',$res);
        return $this->fetch('addknows');
    }
    /*
     *获取分类id
     */
    public function getKnowsId(){
        $data['parent_id']=input('areaId',0);
        $data['is_del']=0;
        $model = new KnowsModel();
        $row = $model->getKnowsId($data);
        print_r(json_encode($row));
    }
    /*
     *添加内容（操作）
     */
    public function addKnows(){
        $album_url = input('album_url');
        $data = [
            'staff_id' => Session::get("STAFF.staff_id"),
            'knows_type_id' => input('knows_type_id2'),
            'parenting_title' => input('parenting_title'),
            'parenting_content' => input('parenting_content'),
            'album_url' => $album_url,
            'knows_type_id1' => input('id'),
            'knows_type_id2' => input('knows_type_id2'),
            'is_home' => input('is_home'),
            'is_vouch' => input('is_vouch'),
            'is_top' => input('is_top'),
            'know_title' => input('know_title'),
            'know_key' => input('know_key'),
            'know_describe' => input('know_describe'),
            'sorts' => input('sorts'),
            'knows_time' => time()
        ];
        $model = new KnowsModel();
        $row = $model->insertRow($data);
        return $row;
    }

    /*
     *上传图片
     */
    public function addKnowsList(){
        if(Request::instance()->isPost()){
            $file = request()->file('img');
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload'.DS.'knows');

            if($info){
                echo $path ='upload'.'/'.'knows'.'/'.date('Ymd').'/'.$info->getFilename();
            }else{
                echo $file->getError();
            }
        }
    }
    /*
     *修改内容显示
     */
    public function updateKnowsView(){
        $id = input('id');
        $model = new KnowsModel();
        $res = $model->select();
        $row = $model->selectFind($id);
        $this->assign('res',$res);
        $this->assign('row',$row);
        return $this->fetch('updateknows');
    }
    /*
     *修改内容（操作）
     */
    public function updateKnowsList(){
        $album_url = input('album_url');
     $data = [
            'id' =>input('ids'),
            'staff_id' => Session::get("STAFF.staff_id"),
            'knows_type_id' => input('knows_type_id2'),
            'parenting_title' => input('parenting_title'),
            'parenting_content' => input('parenting_content'),
            'album_url' => $album_url,
            'knows_type_id1' => input('id'),
            'knows_type_id2' => input('knows_type_id2'),
            'is_home' => input('is_home'),
            'is_vouch' => input('is_vouch'),
            'is_top' => input('is_top'),
            'know_title' => input('know_title'),
            'know_key' => input('know_key'),
            'know_describe' => input('know_describe'),
            'sorts' => input('sorts'),
            'knows_time' => time()
        ];
        $model = new KnowsModel();
        $row = $model->saveFind($data);
        return $row;
    }

    /*
     *育儿知识内容单个删除（逻辑）
     */
    public function delKnows(){
        $id = input('id');
        $model = new KnowsModel();
        $res = $model->deleteFind($id);
        return $res;
    }

    /*
     *多条删除(逻辑)
     */
    public function deleteBatch(){
        $id = input('ids');
        $ids = explode(',',$id);
        foreach ($ids as $id){
            $model = new KnowsModel();
            $res[] = $model->deleteAll($id);
        }
        return $res;
    }


    /*
     *首页设置
     */
    public function setHome(){
        $id = input('id');
        $arr = explode(',',$id);
        foreach($arr as $id){
            $res = new KnowsModel();
            $res = $res->getHome($id);
        }
        return $res;
    }
    /*
     * 首页取消设置
     */
    public function cancelHome(){
        $id = input('id');
        $arr = explode(',',$id);
        foreach($arr as $id){
            $res = new KnowsModel();
            $res = $res->getCancelHome($id);
        }
        return $res;
    }

    /*
    *推荐设置
    */
    public function setRecom(){
        $id = input('id');
        $arr = explode(',',$id);
        foreach($arr as $id){
            $res = new KnowsModel();
            $res = $res->getRecom($id);
        }
        return $res;
    }
    /*
     * 取消推荐设置
     */
    public function cancelRecom(){
        $id = input('id');
        $arr = explode(',',$id);
        foreach($arr as $id){
            $res = new KnowsModel();
            $res = $res->getCancelRecom($id);
        }
        return $res;
    }

    /*
     * 置顶设置
     */
    public function setTop(){
        $id = input('id');
        $arr = explode(',',$id);
        foreach($arr as $id){
            $res = new KnowsModel();
            $res = $res->getTop($id);
        }
        return $res;
    }
    /*
     * 取消置顶设置
     */
    public function cancelTop(){
        $id = input('id');
        $arr = explode(',',$id);
        foreach($arr as $id){
            $res = new KnowsModel();
            $res = $res->getCancelTop($id);
        }
        return $res;
    }


}
